<template>
  <MainLayout 
    :loading="loading" 
    :loadingType="loadingType" 
    :progress="progress"
  >
    <template v-slot:form>
      <a-tabs v-model:activeKey="activeKey">
        <a-tab-pane key="maven" tab="MAVEN">
          <div class="form-container">
            <PackingFormMaven :loading="loading" @finish="onMavenFinish" />
          </div>
        </a-tab-pane>
        <a-tab-pane key="npm" tab="NPM">
          <div class="form-container">
            <PackingFormNpm :loading="loading" @finish="onNpmFinish" />
            <a-button
              v-if="finish"
              type="text"
              style="width: 100%; margin-top: 20px"
              @click="openResult"
            >
              查看结果
            </a-button>
          </div>
        </a-tab-pane>
      </a-tabs>
    </template>
    <template v-slot:log>
      <log-code-mirror v-model="logContent" />
      <PackingResult v-model:visible="showResult" :stat="resultStat" :errorList="resultErrorList" />
    </template>
  </MainLayout>
</template>

<script setup>
import { ref, computed } from 'vue'
import { notification } from 'ant-design-vue'
import MainLayout from './MainLayout.vue'
import PackingFormMaven from './PackingFormMaven.vue'
import PackingFormNpm from './PackingFormNpm.vue'
import PackingResult from './PackingResult.vue'

const activeKey = ref('maven')
const loading = ref(false)
//进度
const progress = ref({})
//日志
const logContent = ref(``)
//下载结果
const finish = ref(false)
const showResult = ref(false)
const resultStat = ref({})
const resultErrorList = ref([])

const loadingType = computed(() => {
  return activeKey.value === 'maven' ? 'spin' : 'progress'
})

window.electronAPI.onPackingLog((value) => {
  logContent.value = logContent.value + value
})

window.electronAPI.onUnPackingProgress((value) => {
  progress.value = value
})

const onMavenFinish = async (values) => {
  loading.value = true
  logContent.value = ''
  const packingParams = {
    mavenHome: values.mavenHome,
    localRepository: values.localRepository,
    pomPath: values.pomPath,
    mirrorUrl: values.mirror.url
  }
  const type = activeKey.value
  const { isSuccess } = await window.electronAPI.packing(type, packingParams)
  loading.value = false
  if (isSuccess) {
    notification.success({
      message: '操作成功',
      placement: 'topLeft'
    })
  } else {
    notification.error({
      message: '操作失败',
      placement: 'topLeft',
      description: '请查看日志获取错误原因'
    })
  }
}

const onNpmFinish = async (values) => {
  progress.value = {}
  loading.value = true
  logContent.value = ''
  const type = activeKey.value

  const packingParams = {
    packageLockPath: values.packageLockPath,
    repositoryDir: values.repositoryDir,
    mirrorUrl: values.mirror.url
  }
  const result = await window.electronAPI.packing(type, packingParams)
  loading.value = false

  resultStat.value = {
    totalCount: result.totalCount,
    errorCount: result.errorCount
  }
  resultErrorList.value = result.errorList.map(({ packageInfo, logText }) => {
    return { ...packageInfo, logText }
  })
  showResult.value = true
  finish.value = true;
}

const openResult = () => {
  showResult.value = true;
}
</script>
<style scoped>
.form-container {
  width: 35vw;
  height: 50vh;
}
</style>
